package com.aqarat.managedBean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.aqarat.controller.AqaratController;
import com.aqarat.controller.AqaratControllerImpl;
import com.aqarat.entity.Apartment;
import com.aqarat.entity.Building;
import com.aqarat.entity.Floor;
import com.core.managedBean.BaseBean;

/**
 * @author karimsherif
 * 
 */
@ManagedBean
@ViewScoped
public class ApartmentBean extends BaseBean {
	private static final long serialVersionUID = 1L;
	private Apartment apartment;
	private AqaratController aqaratController;
	private List<Building> buildingList;
	private List<Floor> floorList;
	private List<Apartment> apartmentList;
	private Building building;
	private Floor floor;
	
	@PostConstruct
	public void loadData() {
		if (aqaratController == null) {
			aqaratController = new AqaratControllerImpl();
		}
		if (apartment == null) {
			apartment = new Apartment();
		}
		if (building == null) {
			building = new Building();
		}

	}

	public void loadBuildingList() {
		try {
			buildingList=aqaratController.getBuildingList();
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("An Error Occured");
		}

	}
	public void loadFloorList() {
		try {
			floorList=aqaratController.getFloorListByBuildingId(building.getRecid());
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("An Error Occured");
		}
	}
	public void loadApartmentList() {
		try {
			apartment=new Apartment();
			floor.setBuilding(building);
			apartment.setFloor(floor);
			apartmentList=aqaratController.getApartmentList(apartment);
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("An Error Occured");
		}
	}
	
	public void saveBuilding() {
		try {
			aqaratController.saveApartment(apartment);
			super.addInfoMessage("Successfully Saved");
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("An Error Occured when Saving");
		}
	}

	public Apartment getApartment() {
		return apartment;
	}

	public void setApartment(Apartment apartment) {
		this.apartment = apartment;
	}

	public AqaratController getAqaratController() {
		return aqaratController;
	}

	public void setAqaratController(AqaratController aqaratController) {
		this.aqaratController = aqaratController;
	}

	public void setBuildingList(List<Building> buildingList) {
		this.buildingList = buildingList;
	}

	public List<Building> getBuildingList() {
		return buildingList;
	}

	public void setFloorList(List<Floor> floorList) {
		this.floorList = floorList;
	}

	public List<Floor> getFloorList() {
		return floorList;
	}

	public void setBuilding(Building building) {
		this.building = building;
	}

	public Building getBuilding() {
		return building;
	}

	public void setFloor(Floor floor) {
		this.floor = floor;
	}

	public Floor getFloor() {
		return floor;
	}

	public void setApartmentList(List<Apartment> apartmentList) {
		this.apartmentList = apartmentList;
	}

	public List<Apartment> getApartmentList() {
		return apartmentList;
	}

}
